define(function(require,exports,module) {
	// require('./plug/hammer.min');
	// require('./plug/layer/layer');
	var myPub = require('./public');

    if(userSource == "MEMBER_SOURCE" && isBind != "true"){
        loginLayer();
    }

    //提示框
    function TipLayer (sCont,sStyle,fn,endFn) {
        layer.open({
            content: sCont,
            style:sStyle || 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px; min-width:50%',
            scrollbar: false,
            time: 2,
            shadeClose: false,
            success:function() {if(fn)fn();},
            end:function () { if(endFn) endFn();}
        });
    }

    function loginLayer() {
        var sMemberLogin = $('.js_memberLoginBox').html();
        //会员登录框显示
        myPub.TipBtnLayer(sMemberLogin,'会员登录','登录',function() {
            var oLayermcont = $('.layermcont');
            var oLoginPhoneNum = oLayermcont.find('.js_loginPhoneNum');			//手机号码框
            var oLoginCode = oLayermcont.find('.js_loginCode');			//手机号码框

            //点击登录回调
            ajaxBindLogin(oLoginPhoneNum.val(),oLoginCode.val());
        },function() {
            //点击关闭按钮回调

        },function() {
            //出现登录框回调
            MemberLoginVer();
            $('.laymshade').on('touchmove',function() {return false});
        })
    }

	//登录验证
	function MemberLoginVer() {
		var oLayermcont = $('.layermcont');

		 /******************   账号验证 start        ***********************/
		var oLoginPhoneNum = oLayermcont.find('.js_loginPhoneNum');			//手机号码框
		var oGetCode = oLayermcont.find('.js_getCode');						//验证码按钮
		var oGetCodeDisabled = 	oLayermcont.find('.js_getCodeDisabled');	//验证码倒计时
		var oErrorTip_p = oLayermcont.find('.js_errorTip p');					//错误提示


		//手机输入框
		oLoginPhoneNum.on('input propertychange',function(){
			phonemobile();
			var _oClearBtn = $(this).siblings('.js_clearBtn');
			$(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
		});

		//验证码
		oGetCode.hammer().on('tap',function(){
			var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');

			if(oLoginPhoneNum.val() == '') {
				oErrorTip_p.text('请输入手机号！')
			}
			else if(oLoginMob.hasClass('has_error')) {
				oErrorTip_p.text('请输入手机号！')
			}
			else {
                console.log(oLoginPhoneNum.val());
                console.log($('#phone_number').length);
                $('#phone_number').attr('value',oLoginPhoneNum.val());
                ajaxSendCode(oLoginPhoneNum.val());
			}

		});

		/*验证手机号码*/
		function phonemobile() {
			var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');
			myPub.checkMobile(oLoginPhoneNum,function() {
				//oLoginMob.removeClass('has_error').removeClass('has_success');
				oErrorTip_p.text('电话号码不能为空');
			},function() {
				//oLoginMob.addClass('has_error').removeClass('has_success');
				oErrorTip_p.text('手机号码有误,请重新填写');

			},function() {
				//oLoginMob.addClass('has_success').removeClass('has_error');
				oErrorTip_p.text('')
			});
		}

		/*倒计时*/
		function setCode() {
			oGetCode.hide();
			oGetCodeDisabled.show();
			myPub.CutTime(90,function(){
				oGetCodeDisabled.hide();
				oGetCode.show().text('重新发送');
			},function(time){
				oGetCodeDisabled.text(time+'s后重发');
			});
		}

        function ajaxSendCode(mobile) {
            $.ajax({
                url: "/sf/sendCode",
                data: {mobile: mobile},
                type: 'GET',
                dataType: "json",
                success: function (data) {
                    if (data.status == "success") {
                        setCode();
                        // myPub.TipLayer(data.message);
                    } else {
                        // myPub.TipLayer(data.message);
                        TipLayer(data.message,null,null,function () {
                            loginLayer();
                        });
                    }
                }, error: function (x) {
                }
            });
        }
	}

    function ajaxBindLogin(mobile,code) {
        console.log(mobile + "  === " + code);
        $.ajax({
            url: "/sf/bind",
            data: {mobile: mobile,code: code},
            type: 'POST',
            dataType: "json",
            success: function (data) {
                if (data.status == "success") {
                    // myPub.TipLayer(data.message);
                    TipLayer(data.message,null,null,function () {
                    });
                } else {
                    // myPub.TipLayer(data.message);
                    TipLayer(data.message,null,null,function () {
                        loginLayer();
                    });
                }
            }, error: function (x) {
            }
        });
    }


});